package com.qi.air.controller;

import com.qi.air.config.Result;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @author zjq
 * @version 1.0
 * @date 2021/9/16 20:50
 */
@RestController
public class uploadController {

    private Logger logger = LoggerFactory.getLogger("uploadController");

    @PostMapping("upload")
    public Result uploadFile(@RequestParam("File") MultipartFile[] multipartFile) throws IOException {
        System.out.println("上传文件的个数为：" + multipartFile.length);
        if (multipartFile.length == 0) {
            return Result.fail("没有选择文件上传");
        }
        String path = "/Users/jiaqi.zhang/Desktop/upload";
        for (MultipartFile file : multipartFile) {
            String originalFilename = file.getOriginalFilename();
            String saveFile = new SimpleDateFormat("yyyy-MM-dd").format(new Date()) + "-" + originalFilename;
            logger.info(originalFilename + "上传成功...");
            file.transferTo(new File(path, saveFile));
            logger.info("保存成功，当前文件保存路径为：" + path);
        }
        return Result.succ("上传成功");
    }


}
